//
//  AppDelegate.m
//  MedicineBox
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "NearbyViewController.h"
#import "UserViewController.h"
#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"
#import "UITabBar+BarTintColor.h"
#import "UITabBar+NightVersion.h"
#import "UMSocial.h"
#import <objc/runtime.h>
#import "GuideViewController.h"
#import "SaveTools.h"
#import "ConsultViewController.h"
#import "AsyncConnection.h"
#import <BaiduMapAPI/BMapKit.h>
@interface AppDelegate () <GuideViewControllerDelegate>

@property (nonatomic, retain)BMKMapManager *mapManager;
@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    // 注册key,绑定buid//
    self.mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [_mapManager start:@"Ka9SuAYZhs8hbtDabj4xOGYw"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    
    
    
    
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"lead"]) {
        // 第一次进入app, 先进入引导页
        GuideViewController *lead = [[GuideViewController alloc] init];
        lead.delegate = self;
        self.window.rootViewController = lead;
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"lead"];
        [lead release];
    }else
    {
        [self createHome];
    }
    
    [NSThread sleepForTimeInterval:3.0];
    
    return YES;
}

- (void)onDoneButtonPressed
{
    [self createHome];
}

- (void)createHome
{
    HomeViewController *home = [[HomeViewController alloc] init];
    UINavigationController *hNav = [[UINavigationController alloc] initWithRootViewController:home];
    UIImage *hoImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yz-shouye" ofType:@"png"]];
    UIImage *homeImage = [hoImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *selHoImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yz-shouye-2" ofType:@"png"]];
    UIImage *selHomeImage = [selHoImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    home.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:homeImage selectedImage:selHomeImage];
    

    
    
    ConsultViewController *consult = [[ConsultViewController alloc] init];
    UINavigationController *consultNaVC = [[UINavigationController alloc] initWithRootViewController:consult];
    
    UIImage *infImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yz-zixun" ofType:@"png"]];
    UIImage *informationImage = [infImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *selInfImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yz-zixun-2" ofType:@"png"]];
    UIImage *selInformationImage = [selInfImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    consult.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"资讯" image: informationImage selectedImage:selInformationImage];
    
    
    NearbyViewController *near = [[NearbyViewController alloc] init];
    UINavigationController *nearNaVC = [[UINavigationController alloc] initWithRootViewController:near];
    
    UIImage *neaImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yz-nearby" ofType:@"png"]];
    UIImage *nearbyImage = [neaImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *selNeaImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yz-nearby-2" ofType:@"png"]];
    UIImage *selNearbyImage = [selNeaImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    near.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"附近" image:nearbyImage  selectedImage:selNearbyImage];
    
    
    UserViewController *use = [[UserViewController alloc] init];
    UINavigationController *nUse = [[UINavigationController alloc] initWithRootViewController:use];
    
    UIImage *myImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yz-wode" ofType:@"png"]];
    UIImage *mmyImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *selMyImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yz-wode-2" ofType:@"png"]];
    UIImage *selMmyImage = [selMyImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    use.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:mmyImage selectedImage:selMmyImage];
    
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    self.window.rootViewController = tabBar;
    tabBar.tabBar.translucent = NO;
    tabBar.tabBar.barTintColor = [UIColor colorWithRed:86/255.0 green:137/255.0 blue:156/255.0 alpha:1];
    tabBar.viewControllers = @[hNav, nearNaVC, consultNaVC, nUse];

    
    [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,nil]forState:UIControlStateSelected];

    
//    tabBar.viewControllers = @[hNav,near,nUse];
    [home release];
    [consult release];
    [consultNaVC release];
    [hNav release];
    [near release];
    [nearNaVC release];
    [use release];
    [tabBar release];
    
    [UMSocialData setAppKey:@"55e0ffc5e0f55a9a44002606"];
    
    NSArray *array = home.tabBarController.tabBar.subviews;
    int flag = 0;
    for (UIView *view in array) {
        NSArray *brray = [view subviews];
        for (UIView *barView in brray) {
            NSString *clasName = [NSString stringWithUTF8String:object_getClassName(barView)];
            if ([clasName isEqualToString:@"UITabBarSwappableImageView"]) {
                UIImageView *imageView = (UIImageView *)barView;
                switch (flag) {
                    case 0:
                        imageView.nightImage = [self valueWithImageName:@"yz-shouye" type:@"png"];
                        flag++;
                        break;
                    
                    case 1:
                        imageView.nightImage = [self valueWithImageName:@"yz-nearby" type:@"png"];
                        flag++;
                        break;
                    case 2:
                        imageView.nightImage = [self valueWithImageName:@"yz-zixun" type:@"png"] ;
                        flag++;
                        break;
                    case 3:
                        imageView.nightImage = [self valueWithImageName:@"yz-wode" type:@"png"] ;
                        flag++;
                        break;
                    default:
                        break;
                }
                
                
            }
            
        }
    }
}

//屏蔽系统颜色
- (UIImage *)valueWithImageName:(NSString *)name type:(NSString *)type
{
    
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:type]];
    UIImage *newImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return newImage;
}

// 本地通知回调函数，当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"noti:%@",notification);
    
    // 获取通知所带的数据
    NSString *notMess = [notification.userInfo objectForKey:@"key"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知"
                                                    message:notMess
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    
    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
    
    
    
}




- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
